package qc.module.platform.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qc.common.core.exception.QCPromptException;
import qc.module.platform.dto.api.APIDto;
import qc.module.platform.dto.api.APIQueryConditionDto;
import qc.module.platform.dto.api.APITreeDto;
import qc.module.platform.service.ApiService;

import java.util.List;

/**
 * ApiController
 *
 * @author QuCheng Tech
 * @create 2024/5/6
 */
@RestController
@RequestMapping("/api")
public class ApiController {
    
    @Autowired
    private ApiService service;

    /**
     * 查询API接口信息
     * 
     * @param condition 查询条件
     * @return List<APIDto>
     * @author QcCheng Tech
     * @since 2024/5/6
     */
    @RequestMapping(value = "query",method = {RequestMethod.GET,RequestMethod.POST})
    public List<APIDto> query(@RequestBody APIQueryConditionDto condition){
        return service.query(condition);
    }

    /**
     * 查询API树型信息
     * 父级为分组，子级为api信息
     *
     * @param condition 查询条件
     * @return List<APIDto>
     * @author QcCheng Tech
     * @since 2024/5/10
     */
    @RequestMapping(value = "/tree",method = {RequestMethod.GET,RequestMethod.POST})
    public List<APITreeDto> queryTree(@RequestBody APIQueryConditionDto condition) throws QCPromptException {
        return service.queryTree(condition);
    }

    /**
     * 获取指定API信息
     * 
     * @param id 唯一标识
     * @return APIDto
     * @author QuCheng Tech
     * @since 2024/5/6
     */
    @GetMapping("/get")
    public APIDto getByParam(@RequestParam String id){
        return service.get(id);
    }

    /**
     * 获取指定API信息
     * 
     * @param id 唯一标识
     * @return APIDto
     * @author QuCheng Tech
     * @since 2024/5/6
     */
    @GetMapping("/get/{id}")
    public APIDto getByPath(@PathVariable String id){
        return service.get(id);
    }

    /**
     * 新增
     * 
     * @param dto 新增dto
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/5/6
     */
    @RequestMapping(value = "/add",method = {RequestMethod.GET,RequestMethod.POST})
    public String add(@RequestBody APIDto dto){
        return service.add(dto);
    }

    /**
     * 修改
     * 
     * @param dto 修改Dto
     * @return 成功返回null，失败返回错误或提示信息
     * @author Qucheng Tech
     * @since 2024/5/7
     */
    @RequestMapping(value = "/update",method = {RequestMethod.POST,RequestMethod.PUT})
    public String update(@RequestBody APIDto dto){
        return service.update(dto);
    }

    /**
     * 删除
     * 
     * @param id 唯一标识
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/5/7
     */
    @DeleteMapping("/delete")
    public String deleteByParam(@RequestParam String id){
        return service.delete(id);
    }

    /**
     * 导入api
     * 
     * @param dtoList 导入的对象集合
     * @return 成功返回null，失败返回错误或提示信息
     * @author QuCheng Tech
     * @since 2024/5/7
     */
    @RequestMapping(value = "/import",method = {RequestMethod.GET,RequestMethod.POST})
    public String importApi(@RequestBody List<APIDto> dtoList){
        return service.importApi(dtoList);
    }
}
